package fl.ass.service.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import cn.cmm.base.framework.utils.BaseLogUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cmm.service.core.framework.biz.BaseBizImpl;
import cmm.service.core.framework.dao.IBaseDao;
import cn.cmm.base.framework.exception.BaseException;
import cn.fl.capital.dto.CapFundDTO;
import cn.fl.capital.service.ICapFundApiService;
import fl.ass.facade.condition.AssDealRecAmtQueryCondition;
import fl.ass.facade.dto.AssDealRecAmtDTO;
import fl.ass.service.dao.AssDealRecAmtMapper;
import fl.ass.service.dao.AssDealRedeemMapper;
import fl.ass.service.dao.AssDealSellMapper;
import fl.ass.service.po.AssDealRecAmt;
import fl.ass.service.service.IAssDealRecAmtService;
import fl.ass.service.transform.AssDealRecAmtTransform;

/**
 * @Description: 处置收款表 服务层接口实现
 * @Description: 类中已经实现了简单增删改查操作,可以直接使用,如果有需要重写该方法即可：
 * @Description: 1、add(TVO entity) 2、removeById(Long id) 3、modify(DTO entity)
 *               4、selectById(Long id) 5、searchListPage(Pager page)
 *               7、searchList(DTO dto)
 * @Description: 业务层方法名称前缀：新增：add* 删除：remove* 修改：modify* 查询对象：select* get*
 *               批量：batch* 查询集合：search*
 */
@Service
public class AssDealRecAmtServiceImpl extends BaseBizImpl<AssDealRecAmt, AssDealRecAmtDTO, AssDealRecAmtQueryCondition>
		implements IAssDealRecAmtService {

	private static final Logger logger = LoggerFactory.getLogger(AssDealRecAmtServiceImpl.class);

	@Autowired
	private AssDealRecAmtMapper assDealRecAmtMapper;

	@Autowired
	private AssDealRedeemMapper assDealRedeemMapper;
	
	@Autowired
	private AssDealSellMapper assDealSellMapper;

	/**
	 * 资金暴露服务
	 */
	@Autowired
	private ICapFundApiService capFundApiService;

	@Override
	public List<AssDealRecAmtDTO> searchList(AssDealRecAmtQueryCondition condition) throws BaseException {
		if (null == condition)
			return new ArrayList<AssDealRecAmtDTO>();
		List<AssDealRecAmt> list = this.assDealRecAmtMapper.selectList(condition);
		return AssDealRecAmtTransform.toDTOList(list);
	}

	@Override
	public IBaseDao<AssDealRecAmt, AssDealRecAmtQueryCondition> getDao() {
		return this.assDealRecAmtMapper;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * fl.ass.service.service.IAssDealRecAmtService#modifyAssDealRectAmt(fl.ass.
	 * facade.dto.AssDealRecAmtDTO)
	 */
	@Override
	public boolean modifyAssDealRectAmt(AssDealRecAmtDTO dto) {
		boolean result = false;
		try {
			result = this.modify(dto);
		} catch (BaseException e) {
			result = false;
            BaseLogUtils.newLogger("异常!" + e.getMessage()).setThrowable(e).info();
			throw e;
		} catch (Exception e) {
			result = false;
            BaseLogUtils.newLogger("异常!" + e.getMessage()).setThrowable(e).info();
			throw e;
		}
		return result;
	}

	/* (non-Javadoc)
	 * @see fl.ass.service.service.IAssDealRecAmtService#batchAddFundInfo(java.lang.Long, java.lang.Long, java.lang.String, java.lang.String)
	 */
	@Override
	public boolean batchAddFundInfo(Long id, Long currentUserId, String currentUserName, String... dataTypes) {
		boolean result = true;
		try {
			String dataType = "redeem";
			if(null != dataTypes && dataTypes.length > 0){
				dataType = dataTypes[0];
			}
			Map<String,Object> data = null;
			if(dataType.equals("redeem")){
				//赎回
				List<Map<String,Object>> datas = assDealRedeemMapper.selectInfoById(id);
				if(null != datas && datas.size() > 0){
					data = datas.get(0);
				}
			}else if(dataType.equals("sell")){
				//变卖
				List<Map<String,Object>> datas = assDealSellMapper.selectInfoById(id);
				if(null != datas && datas.size() > 0){
					data = datas.get(0);
				}
			}else{
				//不做处理
			}
			if(null != data){
				List<AssDealRecAmt> dealRecAmts = assDealRecAmtMapper.selectByDataId(id);
				if (null != dealRecAmts) {
					// 赎回类型-生成来款
					Long cstId = ((BigDecimal)data.get("CSTID")).longValue();
					String cstNm = (String)data.get("CSTNAME");
		            String payeeAccNm = (String)data.get("PAYEEACCNM"); //收款方账户名称
		            String payeeAccNo = (String)data.get("PAYEEACCNO"); //收款方银行账号
		            //String payeeBankNm = (String)data.get("PAYEEBANKNM"); //收款方开户银行
		            //String payeeBankNo = (String)data.get("PAYEEBANKNO"); //收款方开户行号
		            String payschNo = (String)data.get("PAYSCHNO"); //支付表号         
		            String procInstCode = (String)data.get("PROCINSTCODE"); //流程编号
		            if (null != cstId && StringUtils.isNotBlank(cstNm)) {
		            	Long zero = new Long(0);
						List<CapFundDTO> dtos = new ArrayList<CapFundDTO>();
						CapFundDTO capFundDto = null;
						for(AssDealRecAmt dealRecAmt : dealRecAmts){
							if(null != dealRecAmt.getReceivedAmt() && dealRecAmt.getReceivedAmt().compareTo(zero) >= 0 ){
								capFundDto = new CapFundDTO();
								capFundDto.setCstId(cstId);// 客户ID
								capFundDto.setCstNm(cstNm);// 客户名称
								capFundDto.setAcceptCode(payeeAccNo); //收款账号
								capFundDto.setAcceptName(payeeAccNm); //收款人
								capFundDto.setComeCode(dealRecAmt.getPayerAccNo());// 来款账号可为空
								capFundDto.setComeName(dealRecAmt.getPayerAccNm());// 来款户名
								capFundDto.setAmount(dealRecAmt.getReceivedAmt());// 来款金额
								capFundDto.setAvailableAmount(dealRecAmt.getReceivedAmt());// 可用金额
								capFundDto.setAcceptDate(dealRecAmt.getReceivedTm());// 交易时间
								capFundDto.setAcceptActualDate(dealRecAmt.getReceivedTm());// 实际到账时间
								capFundDto.setRelationPayId(id);// 流程中dataId
								capFundDto.setIdentifyOperator(currentUserId); //认款人ID
								capFundDto.setIdentifyOperatorNm(currentUserName);//认款人名称
								capFundDto.setCrtUsrId(currentUserId); //创建人ID
								capFundDto.setDocketRem("流程号:"+procInstCode+",支付表号:"+payschNo); //来款备注说明:流程号+支付表号
								dtos.add(capFundDto);
							}
						}
						if(dtos.size() > 0){
							result = capFundApiService.addCapFund(dtos);
						}
		            }else{
		            	result = false;
                        BaseLogUtils.newLogger("客户id和名称至少有一个为空").setKeys("cstId:" + cstId, "cstNm:" + cstNm).info();
		            }
				}
			}else{
				result = false;
				BaseLogUtils.newLogger("data == null").setKeys("dataType:" + dataType, "id:" + id).info();
			}
		} catch (BaseException e) {
			result = false;
			BaseLogUtils.newLogger("异常!" + e.getMessage()).setThrowable(e).info();
			throw e;
		} catch (Exception e) {
			result = false;
            BaseLogUtils.newLogger("异常!" + e.getMessage()).setThrowable(e).info();
			throw e;
		}
		return result;
	}

}
